<?php
/**
* wish token 操作类
*/
define("BASEPATH","");
define("ENVIRONMENT","");
class wish 
{
	private $host="";
    private $name="";
    private $pass="";
    private $database="";
    private $con="";
    private $table="d_shop";
	
	function __construct()
	{
		require_once(dirname(__FILE__).'/../../application/config/database.php');
        require_once(dirname(__FILE__).'/../../application/config/webset.php');
        $this->host = $db['default']['hostname'];
        $this->name = $db['default']['username'];
        $this->pass = $db['default']['password'];
        $this->database = $db['default']['database'];
        $this->connect();
	}
	//链接数据库
    public function connect(){
         $this->con = mysqli_connect($this->host,$this->name,$this->pass) or die ($this->error());
          mysqli_select_db($this->con,$this->database) or die("没该数据库：".$this->database);
     }
    //更新token信息
   public function updatetoken($wishtoken,$id){
   $sql="update {$this->table} set amazonkey='{$wishtoken}' where id = '{$id}';";
   mysqli_query($this->con,$sql);
   }
  //获取数据库中的wish信息
   public function getwish($id){
    $sql="select amazonkey from {$this->table} where id ='{$id}';";
    $result  = mysqli_query($this->con,$sql);
    if (mysqli_num_rows($result) < 1) return '{"code":"1","data":"no data"}';
    while ($row=mysqli_fetch_array($result)) {
    	$amazonkey=$row['amazonkey'];
    }

    if (strlen(trim($amazonkey))==0) return '{"code":"2","data":"amazonkey is empty"}';

    return '{"code":"0","data":'.$amazonkey.'}';
   }
   //获取token值
  public function gettoken($code,$wish){
 $wisharr=json_decode($wish,true);
$client_id = urlencode($wisharr['wishkey']);
$client_secret = urlencode($wisharr['wishsecret']);
$code = urlencode($code);

$redirect_uri = urlencode($wisharr['wishurl']);
// echo $client_id;
// echo "<br/>";
// echo $client_secret;
// echo "<br/>";
// echo $code;
// die;
$url = sprintf("https://merchant.wish.com/api/v2/oauth/access_token?&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s&grant_type=authorization_code", $client_id, $client_secret, $code, $redirect_uri);

$context = stream_context_create(array(
    'http' => array(
        'method'        => 'POST',
        'ignore_errors' => true,
    ),
));

// Send the request
$response = file_get_contents($url, TRUE, $context);
$resarr=json_decode($response,true);
print_r($resarr);

if($resarr['code']!='0') return 'Failed to get token';
return json_encode($resarr['data']);
// return $resarr;
}  
 
 public function close(){
 	mysqli_close($this->con);
 }

}

//具体操作环节
$id=trim($_GET['id']);
$code=trim($_GET['code']);

// echo $id;
// echo "<br/>";
// echo $code;
// exit;
//$code='18ed380656bf4ff7af5008d159b84f9f';
//$id='373';
// $code='05bb7cb1850d4eab9d3c5be857f484be';
// $id='141';
$wishapi=new wish();
$wishjson=$wishapi->getwish($id);
$wisharr=json_decode($wishjson,true);
$wishcode=$wisharr['code'];

if ($wishcode=='1' || $wishcode == '2') {
	echo $wisharr['data'];
	exit;
}
$wishjson=json_encode($wisharr['data']);
$datajson=$wishapi->gettoken($code,$wishjson);
echo $datajson;
if ($datajson=='Failed to get token') {
  exit;
}
$dataarr=json_decode($datajson,true);
// $dataarr=array("expiry_time"=>"1502980302","token_type "=>"access_token","access_token"=>"d28e80dd0e5848ea9c155c99dd41d86c","expires_in"=>"2591719","reason"=>'',"refresh_token"=>"6727e43234684df1bb1797049e83f187","client_id"=>"596d9b949415850ffdb8538e","token_id"=>"596db95e905fdb30ecb16981","merchant_id"=>"5673a38fd68ccc26da473729","merchant_user_id"=>"5673a1c2c5271926ad4cb90c");
// $wishjson='{"wishkey":"596d9b949415850ffdb8538e","wishsecret":"f60a1ffacfde49b0821aeaf26b8797b8"}';
// $id='141';
$wishsarr=json_decode($wishjson,true);
array_push($wishsarr,$dataarr);
$wishsjson=json_encode($wishsarr);
$wishapi->updatetoken($wishsjson,$id);
$wishapi->close();




// {"wishkey":"5926780bd594f14ad81d04e9","wishsecret":"70034ac1daa84f91bd8e429af71b543a","0":{"expiry_time":1498913146,"token_type ":"access_token","access_token":"cdeef1fb9baa4bedb90b8f6f6a15183b","expires_in":2591973,"reason":null,"refresh_token":"cc1f16e5529349ed84e8448dccf28a8b","client_id":"5926780bd594f14ad81d04e9","token_id":"592faa0af186cf16d5820199","merchant_id":"59267398f186cf52ef48e716","merchant_user_id":"59267398f186cf52ef48e715"}}







?>